package com.hand.prantice.ssm.filter;


import com.hand.prantice.ssm.constant.Test;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author 马旭阳
 * @date2019/7/17
 */
//@Component
//@WebFilter(filterName = "PerFilter",urlPatterns = "/*")
    @Order(99)
public class PerFilter implements Filter {

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        System.out.println("PerFilter init");
    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        HttpServletRequest httpServletRequest = (HttpServletRequest)servletRequest;
        String token = httpServletRequest.getHeader("token");
        HttpServletResponse httpServletResponse = (HttpServletResponse)servletResponse;

        if (validateuserPer(token)) {
            filterChain.doFilter(servletRequest,servletResponse);
            httpServletResponse.setStatus(200);
        }else {
            httpServletResponse.setStatus(403);
        }


    }

    @Override
    public void destroy() {
        System.out.println("PerFilter destroy");
    }

    private boolean validateuserPer(String token){
        if (Test.TEST_CONTENT.equals(token)) {
            return  true;
        }
        return false;
    }
}

